package com.ysx.controller;


import com.itheima.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/getName")
    public Result getName() {
//       调用springSecurity的context方法 ----> 返回当前已经认证封装后的对象
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println("principal" + principal);
//        判断
        Result result=null;
        if(principal!=null){
            result=new Result(true,"获得用户名成功！",principal.getUsername());
        }else {
            result=new Result(false,"获得用户名失败！");
        }
        return result;
    }



}
